<?php
namespace api\wxapp\model;
use api\common\model\CommonModel;
use think\Model;

class SchoolCourseModel extends CommonModel
{    
    public function schoolClass()
    {
        return $this->belongsTo('app\school\model\SchoolClassModel', 'class_id')->where('delete_time',0)->order('list_order ASC') ;
    }
    public function schoolLevel()
    {
        return $this->belongsTo('app\school\model\SchoolLevelModel', 'level_id')->where('delete_time',0)->order('list_order ASC') ;
    }
    public function schoolSubject()
    {
        return $this->belongsTo('app\school\model\SchoolSubjectModel', 'subject_id')->where('delete_time',0)->order('list_order ASC') ;
    }
    public function schoolTeacher()
    {
        return $this->belongsTo('app\school\model\SchoolTeacherModel', 'teacher_id')->where('delete_time',0)->order('list_order ASC') ;
    }
    
    /**
     * post_content 自动转化
     * @param $value
     * @return string
     */
    public function getContentAttr($value)
    {
        return cmf_replace_content_file_url(htmlspecialchars_decode($value));
    }

    /**
     * post_content 自动转化
     * @param $value
     * @return string
     */
    public function setContentAttr($value)
    {
        return htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($value), true));
    }
    /**
     * more 自动转化
     * @param $value
     * @return array
     */

    public function getThumbnailAttr($value)
    {       
        if (!empty($value)) {
            $value = cmf_get_image_url($value);
        }

        return $value;
    }
    
    public static function recommendedList($next_id = 0, $num = 10)
    {
        $limit = "{$next_id},{$num}";       
        $list  = self::with(['schoolClass','schoolSubject','schoolTeacher'])     
            ->where(['delete_time'=>0,'recommended'=>1,'status'=>1])
            ->order('list_order ASC')       
            ->limit($limit)
            ->select();
        return $list;
    }

    public static function newList($next_id = 0, $num = 10)
    {
        $limit = "{$next_id},{$num}";       
        $list  = self::with(['schoolClass','schoolSubject','schoolTeacher'])           
            ->where(['delete_time'=>0,'status'=>1,'is_new'=>1])
            ->order('list_order ASC')
            ->limit($limit)->select();
        return $list;
    }
}